package com.example.mapper;

import com.example.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoXmlMapper2 {
    Integer insertUser(UserInfo userInfo);


    @Insert("<script>" +
            "INSERT INTO user_info (username, password, age" +
            "<if test='gender != null'>, gender</if>" +
            "<if test='phone != null'>, phone</if>" +
            ") VALUES (#{username}, #{password}, #{age}" +
            "<if test='gender != null'>, #{gender}</if>" +
            "<if test='phone != null'>, #{phone}</if>" +
            ")" +
            "</script>")
    Integer insertUserDynamic(UserInfo userInfo);



    @Insert("<script>" +
            "INSERT INTO user_info " +
            "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
            "<if test=\"username != null\">username,</if>" +
            "<if test=\"password != null\">password,</if>" +
            "<if test=\"age != null\">age,</if>" +
            "<if test=\"gender != null\">gender,</if>" +
            "<if test=\"phone != null\">phone,</if>" +
            "</trim> VALUES " +
            "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
            "<if test=\"username != null\">#{username},</if>" +
            "<if test=\"password != null\">#{password},</if>" +
            "<if test=\"age != null\">#{age},</if>" +
            "<if test=\"gender != null\">#{gender},</if>" +
            "<if test=\"phone != null\">#{phone},</if>" +
            "</trim>" +
            "</script>")
    Integer insertUserByCondition2(UserInfo userInfo);

//    List<UserInfo> queryByConditionError(UserInfo userInfo);


    @Select("<script>" +
            "SELECT id, username, age, gender, phone, delete_flag, create_time, update_time " +
            "FROM user_info " +
            "<where>" +
            "    <if test=\"age != null\">AND age = #{age}</if>" +
            "    <if test=\"gender != null\">AND gender = #{gender}</if>" +
            "    <if test=\"deleteFlag != null\">AND delete_flag = #{deleteFlag}</if>" +
            "</where>" +
            "</script>")
    List<UserInfo> queryByCondition3(UserInfo userInfo);


    Integer updateUserError(UserInfo userInfo);


    @Update("<script>" +
            "UPDATE user_info " +
            "<set>" +
            "<if test=\"username != null\">username = #{username},</if>" +
            "<if test=\"age != null\">age = #{age},</if>" +
            "<if test=\"gender != null\">gender = #{gender},</if>" +
            "<if test=\"deleteFlag != null\">delete_flag = #{deleteFlag},</if>" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    Integer updateUserByCondition(UserInfo userInfo);


    // 方式1：使用List
    @Delete("<script>" +
            "DELETE FROM user_info WHERE id IN " +
            "<foreach collection=\"list\" item=\"id\" open=\"(\" close=\")\" separator=\",\">" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    void deleteByIds(List<Integer> ids);

    // 方式2：使用数组
    void deleteByIds2(Integer[] ids);

    void deleteByIds3(@Param("idList") List<Integer> ids);

    Integer batchInsert(List<UserInfo> userInfoList);


    List<UserInfo> selectAll();
    List<UserInfo> selectByCondition(UserInfo condition);


}
